<?
/**
*  客户管理
*  2013/7/14 by zh
*/
include("../webkit/include/simple_db.php");

include("session.php");
require("ajax_base.php");

$db = new SimpleDB();
$action = $_GET['action'];

if($action == "del") {
	$db->doDelete('jx_client', 'id='.$_GET['id']); 
	echo "ok";

} else if($action == "list") {
	$condition = "1=1";
	if(isset($_GET['client_type']) && $_GET['client_type']!=0) {
		$condition .= " and client_type=".$_GET['client_type'];
	}
	if(isset($_GET['client_name'])) {
		$condition .= " and client_name like '%".$_GET['client_name']."%'";
	}
	if(isset($_GET['telephone'])) {
		$n = $_GET['telephone'];
		$condition .= " and (mobilephone='$n' or telephone1='$n' or telephone2='$n' or telephone3='$n')";
	}
	$sql = "select id,client_name,mobilephone,client_no,client_type,address, exp_date, telephone1,telephone2,telephone3, '' as op_link   
	from jx_client where $condition order by id desc"; 
	echo to_grid_json($db, $sql, $dic_data, array("client_name"=>"blue_color_mapper","exp_date"=>"red_color_mapper", "op_link"=>"op_link_mapper"));

} else if($action == "get") {
	$sql = "select * from jx_client where id=".$_GET['id']; 
	$arr = $db->getOneRecordArray($sql);
	echo array_to_json($arr);

} else if($action == "get_client_by_phone") {
	$sql = "select * from jx_client where mobilephone='".$_GET['number']."' or telephone1='".$_GET['number']."'"; 
	$arr = $db->getOneRecordArray($sql);
	if($arr == null) {
		echo "no record";
	} else {
		echo array_to_json($arr);
	}

} else if($action == "get_client_by_code") {
	$sql = "select * from jx_client where client_no='".$_GET['code']."'"; 
	$arr = $db->getOneRecordArray($sql);
	if($arr == null) {
		echo "no record";
	} else {
		echo array_to_json($arr);
	}

} else if($action == "add") {
	unset($_POST['id']);
	$db->doInsert('jx_client', $_POST);
	echo 'ok,'. mysql_insert_id();

} else if($action == "edit") {
	$id = $_POST['id'];
	unset($_POST['id']);
	$db->doUpdate('jx_client', $_POST, 'id='.$id);
	echo 'ok';

}  else if($action == "import") {
	$f = fopen($_FILES['upload_file']['tmp_name'], "r");
	$i = 0;
	while(($line=fgets($f)) != "") {
		$arr = explode(",", $line);
		if(!isset($arr[6])) {
			$arr[6] = 1;
		}
		$rec = array(
			'client_no'=>$arr[2],
			'client_name'=>$arr[0],
			'mobilephone'=>$arr[3],
			'telephone1'=>$arr[4],
			'client_type'=>$arr[6],
			'address'=>$arr[1], 
			'exp_date'=>$arr[5]
		);
		$db->doInsert('jx_client', $rec);
		$i++;
	}
	echo "<script> parent.upload_completed($i);</script>"; // 提示记录导入条数
}



function op_link_mapper($v, $row) {
	return "<a href=\"order_list.php?client_id=".$row['id']."\">查看工单</a>";
	//return "<a href=\"javascript:parent.toPage(\'order_list.php?client_id=".$row['id']."\',\'工单列表\')\">查看</a>";
}
?>